#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main()
{
	int sockfd ;
	struct sockaddr_in addr;
	short PORT = 1234;
	char *IP = "127.0.0.1";
	printf("准备启动服务器...\n");
	if((sockfd = socket(PF_INET , SOCK_STREAM , 0)) == -1)
		perror("socket创建失败") , exit(-1);
	memset(&addr , 0 , sizeof(addr));
	addr.sin_family = PF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr = INADDR_ANY;

	int reuseaddr = 1;
	setsockopt(sockfd , SOL_SOCKET, SO_REUSEADDR,&reuseaddr,sizeof(reuseaddr));

	if(bind(sockfd , (struct sockaddr *)&addr , sizeof(addr)) == -1)
		perror("服务器绑定失败") , exit(-1);
	listen(sockfd,5);
	printf("服务器成功启动...\n");
	
	struct sockaddr_in addrclient;
	int len = sizeof(addrclient);
	while(1)
	{
		int newsock = accept(sockfd , (struct sockaddr *)&addrclient , &len);
		
		char buffer[200] = {};
		while(recv(newsock , buffer , sizeof(buffer) , 0) > 0)
		{
			printf("buffer = %s\n",buffer);
			memset(buffer , 0x00 , sizeof(buffer));
		}
	}	
	close(sockfd);
	printf("关闭服务器...\n");
	exit(0);
}
